package com.hy.rabbitmq.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:
 * User:Mr.Du
 * Date:2023/7/20
 * Time:21:07
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/mq")
public class MsgController {

    private final RabbitTemplate rabbitTemplate;

    @RequestMapping("/send")
    public String sendMsg(@RequestParam("msg") String msg){
        rabbitTemplate.convertAndSend("test-exchange", "test-routingKey", msg);
        return "已发送";
    }


    @RabbitListener(queues = "test-queue")
    public void receiveMsg(String msg){
        log.info("收到 RabbitMQ 消息：{}", msg);
    }

}
